package com.vichat.gps.controller;

import com.alibaba.fastjson.JSONObject;
import com.vichat.common.cache.RedisHelper;
import com.vichat.gps.entity.LocationData;
import com.vichat.gps.service.IGpsService;
import com.vichat.user.entity.UserBasic;
import com.vichat.user.service.IUserService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

public class SaveGpsThread implements Runnable {

    private static Logger logger = LoggerFactory.getLogger(SaveGpsThread.class);

    IGpsService gpsService;
    GpsAppCache gpsAppCache;

    public SaveGpsThread(IGpsService gpsService_, GpsAppCache gpsAppCache_) {
        logger.info(System.currentTimeMillis() + " I am started ");
        this.gpsService = gpsService_;

        this.gpsAppCache = gpsAppCache_;
    }

    @Override
    public void run() {
        while (true)
            try {
                List<LocationData> locationDatas = gpsAppCache.get(1000);
                if (locationDatas != null && locationDatas.size() > 0) {
                    gpsService.saveBatchLocation(locationDatas);
                    logger.info(System.currentTimeMillis() + " save locations " + locationDatas.size());
                } else {
                    //logger.info(System.currentTimeMillis() + " no locations ");
                    Thread.sleep(1000 * 5);
                }


            } catch (Exception ee) {
                ee.printStackTrace();
                logger.error(ee.getMessage());
            }

    }


}
